<?php

/**
 * This is the model class for table "{{user}}".
 *
 * The followings are the available columns in table '{{user}}':
 * @property integer $Id
 * @property string $username
 * @property string $userpwd
 * @property integer $sta
 * @property integer $group_id
 * @property string $mobile
 * @property string $email
 * @property string $last_logintime
 * @property string $reg_ip
 * @property string $reg_time
 * @property string $last_loginip
 * @property string $update_time
 * @property string $login_nums
 * @property string $score
 * @property integer $verify_email
 * @property integer $password_strength
 * @property integer $reg_method
 * @property integer $login_method
 * @property string $nickname
 * @property string $pay_score
 */
class User extends AdminActiveRecord
{
	public $connectionPrefix='coodinn';//Do modify it.
	public $verifyCode ='';
	public $autoLogin=false;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user}}';
	}
	
	/**
	 * Modify it if you need.
	 * @return mixed the primaryKey.
	 */
	public function primaryKey()
	{
		return 'id';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('sta, group_id, verify_email, password_strength, reg_method, login_method', 'numerical', 'integerOnly'=>true),
			array('username, last_logintime, nickname', 'length', 'max'=>32),
			array('userpwd, email', 'length', 'max'=>64),
			array('mobile', 'length', 'max'=>25),
			array('reg_ip', 'length', 'max'=>24),
			array('reg_time, update_time', 'length', 'max'=>11),
			array('last_loginip', 'length', 'max'=>23),
			array('login_nums, pay_score', 'length', 'max'=>255),
			array('score', 'length', 'max'=>2),
			//有效性检查
			array('username', 'filter', 'filter' => array($this, 'usernameCanUsed'), 'on' => array('usernameCheck', 'usernameReg', 'emailReg', 'mobileReg','merchantReg')),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('Id, username, userpwd, sta, group_id, mobile, email, last_logintime, reg_ip, reg_time, last_loginip, update_time, login_nums, score, verify_email, password_strength, reg_method, login_method, nickname, pay_score', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'Id' => 'ID',
			'username' => 'Username',
			'userpwd' => 'Userpwd',
			'sta' => 'Sta',
			'group_id' => 'Group',
			'mobile' => 'Mobile',
			'email' => 'Email',
			'last_logintime' => 'Last Logintime',
			'reg_ip' => 'Reg Ip',
			'reg_time' => 'Reg Time',
			'last_loginip' => 'Last Loginip',
			'update_time' => 'Update Time',
			'login_nums' => 'Login Nums',
			'score' => 'Score',
			'verify_email' => 'Verify Email',
			'password_strength' => 'Password Strength',
			'reg_method' => 'Reg Method',
			'login_method' => 'Login Method',
			'nickname' => 'Nickname',
			'pay_score' => 'Pay Score',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('Id',$this->Id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('userpwd',$this->userpwd,true);
		$criteria->compare('sta',$this->sta);
		$criteria->compare('group_id',$this->group_id);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('last_logintime',$this->last_logintime,true);
		$criteria->compare('reg_ip',$this->reg_ip,true);
		$criteria->compare('reg_time',$this->reg_time,true);
		$criteria->compare('last_loginip',$this->last_loginip,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('login_nums',$this->login_nums,true);
		$criteria->compare('score',$this->score,true);
		$criteria->compare('verify_email',$this->verify_email);
		$criteria->compare('password_strength',$this->password_strength);
		$criteria->compare('reg_method',$this->reg_method);
		$criteria->compare('login_method',$this->login_method);
		$criteria->compare('nickname',$this->nickname,true);
		$criteria->compare('pay_score',$this->pay_score,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 邮箱是否被别的用户使用
	 * @return boolean
	 */
	public function emailIsExists($id=0) {
		return $this->exists("email=:email and id<>:id", array(":email" => strtolower($this->email),":id"=>$id));
	}
	
	/**
	 * 用户名是否存在
	 * @return boolean
	 * $user=new User();
	 * $user->username=$username;
	 * $user->check_type=0;
	 * $user->usernameIsExists();
	 */
	public function usernameIsExists() {
		//下面检查存在性
		return $this->exists("username=:username", array(":username" => strtolower($this->username)));
	}
	
	/**
	 * 用户名是否可被使用
	 * @param string $val
	 * @return boolean
	 */
	public function usernameCanUsed() {
		if ($this->usernameIsExists()){
			$this->addError('username', '指定的用户名已存在');
			return false;
		}
		if (in_array($this->username,array('admin','admin888'))){
			$this->addError('username', '指定的用户名限制使用');
			return false;
		}
		return $this->username;
	}
	
	/**
	 * 昵称是否可被使用
	 * @param string $val
	 * @return boolean
	 */
	public function nicknameCanUsed($val) {
		if ($this->nicknameIsExists()) {
			$this->addError('nickname', '指定的昵称已经存在！');
			return false;
		}
		return $this->nickname;
	}
	
	/*
	 * 用户注册
	 */
	public function userRegister($mode='reg'){
		$this->group_id  = 1;  //设置用户类型  1代表前台
		if (!$this->registerSave($mode)){
			return UtilD::handleResult(0, DaoD::getErrorsToString($this,' ','系统繁忙请稍后再试！'));
		}
		return UtilD::handleResult(1, '注册成功');
	}
	
	/*
	 * 保存注册用户
	 */
	public  function registerSave($mode = 'reg'){
		//$this->accountValidator($mode);  //用户注册模式  选择验证器
		$this->setIsNewRecord(true);
	    $this->reg_ip = $this->last_loginip = Yii::app()->request->getUserHostAddress();
	    (is_null($this->reg_time) || !$this->reg_time) && $this->reg_time = time();
	    return $this->save();
	}
	
	/*
	 * 前台登陆
	 */
	public function login(){
		//$this->accountValidator('login');
		if(!$this->validate(null,false)){
			return UtilD::handleResult(0, DaoD::getErrorsToString($this));
		}
		try{
			$identity = new UserIdentity($this->username, $this->userpwd);
		}catch (Exception $e){
			
		}
	}
	
	
	/*
	 * 登陆
	 */
	public function loginAccount($login_method,$username,$userpwd){
		$result = $this->find('login_method=:login and username=:username and userpwd=:pwd',array(':login'=>$login_method,':username'=>$username,':userpwd'=>$userpwd));
		if(is_null($result)) return false;
		$datas = $result->getAttributes();
		$result_data = XxteaD::decrypt ( $datas, Yii::app ()->params ['frontpassportkey']);
		$data = CJSON::decode ( $result_data );
		$data [WebUserD::STORAGE_KEY] = CJSON::decode ( $data [WebUserD::STORAGE_KEY] );
		return $data;
	}
	
	
	
	
	
	
	
	
	
}